package com.qd.panda.invoice;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.common.panda.domain.entity.invoice.UserInvoiceDTO;
import com.qd.panda.service.invoice.UserInvoiceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 发票/APP_个人开票管理
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/invoice/userInvoice/")
@Slf4j
public class UserInvoiceController {
    private final UserInvoiceService userInvoiceService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public UserInvoiceDTO save(@Validated({DefaultGroup.class}) @RequestBody UserInvoiceDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        dto.setAddTime(LocalDateTime.now());
        return userInvoiceService.save(dto);
    }

    /**
     * 修改
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/update")
    public UserInvoiceDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody UserInvoiceDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        return userInvoiceService.update(dto);
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            userInvoiceService.removeById(ids.get(0));
        } else {
            userInvoiceService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserInvoiceDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return userInvoiceService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserInvoiceDTO> page(UserInvoiceDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userInvoiceService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 全部
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<UserInvoiceDTO> list(UserInvoiceDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userInvoiceService.getListDto(dto);
    }

}